<template>
    <div class="comment-post">
        <van-field class="post-field"  rows="2" autosize type="textarea" maxlength="50"
            placeholder="请输入留言" show-word-limit v-model.trim="message"/>
        <van-button @click="onPost" class="post-btn" :disabled="!message.length">发布</van-button>
    </div>
</template>

<script>
import { CommentAPI } from '@/api/comment'
import { ref, inject } from 'vue'
import { Toast } from 'vant'
export default {
  name: 'CommentPost',
  components: {},
  props: {
    target: {
      type: [Number, String, Object],
      required: true
    }
  },
  setup (props, content) {
    const message = ref('')
    const articleId = inject('articleId', 'null')
    const onPost = async () => {
      try {
        const { data } = await CommentAPI({
          target: props.target,
          content: message.value,
          art_id: articleId
        })
        // 关闭弹出层

        // 发布内容置顶

        // 清空文本
        message.value = ''
        // 发布的数据
        content.emit('psot-success', data.data)
        Toast.success('发布成功')
      } catch (err) {
        console.log(err)
      }
    }
    return {
      message,
      onPost,
      articleId
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
    display: flex;
    align-items: center;
    padding: 32px 0 32px 32px;

    .post-field {
        background-color: #f5f7f9;
    }

    .post-btn {
        width: 150px;
        border: none;
        padding: 0;
        color: #6ba3d8;

        &::before {
            display: none;
        }
    }
}
</style>
